﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Milkify.Input
{
    public class GenericKeyBinding : InputBinding
    {
        [System.ComponentModel.TypeConverter(typeof(GenericKeyGestureConverter))]
        [System.Windows.Markup.ValueSerializer(typeof(GenericKeyGestureValueSerializer))]
        public override InputGesture Gesture
        {
            get
            {
                return base.Gesture;
            }
            set
            {
                if (!(value is GenericKeyGesture))
                    throw new ArgumentException("GenericKeyBinding only supports GenericKeyGesture as its InputGesture.");
                base.Gesture = value;
            }
        }

        public Key Key
        {
            get
            {
                var gesture = GetKeyGesture();
                if (gesture == null)
                    return Key.None;
                return gesture.Key;
            }
            set
            {
                var gesture = GetKeyGesture();
                if (gesture == null)
                    Gesture = new GenericKeyGesture(value, ModifierKeys.None);
                else
                    Gesture = new GenericKeyGesture(value, gesture.Modifiers);
            }
        }

        public ModifierKeys Modifiers
        {
            get
            {
                var gesture = GetKeyGesture();
                if (gesture == null)
                    return ModifierKeys.None;
                return gesture.Modifiers;
            }
            set
            {
                var gesture = GetKeyGesture();
                if (gesture == null)
                    Gesture = new GenericKeyGesture(Key.None, value);
                else
                    Gesture = new GenericKeyGesture(gesture.Key, value);
            }
        }

        public GenericKeyBinding()
        {
        }

        public GenericKeyBinding(ICommand command, GenericKeyGesture gesture)
            : base(command, gesture)
        {
        }

        public GenericKeyBinding(ICommand command, Key key, ModifierKeys modifiers)
            : base(command, new GenericKeyGesture(key, modifiers))
        {
        }

        private GenericKeyGesture GetKeyGesture()
        {
            return Gesture as GenericKeyGesture;
        }
    }
}
